package com.six.backend.service.impl;

import com.six.backend.dao.LogDao;
import com.six.backend.pojo.Log;
import com.six.backend.service.LogService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: skyler
 * @Date: 2021/11/21 13:22
 */
@Service
public class LogServiceImpl implements LogService {

    @Autowired
    private LogDao logDao;

    @Override
    public void saveSystemLog(String key, String url) {
        HashMap map = new HashMap<>();
        map.put("id",key);
        map.put("name","SystemLog.log");
        map.put("time",new Date());
        map.put("address",url);
        map.put("type",1);
        logDao.saveLog(map);
    }

    @Override
    public List<Log> selectSystemLog(String name) {
        return logDao.selectSystemLog(name);
    }

    @Override
    public List<Log> selectSystemLog() {
        return logDao.selectSystemLog1();
    }

    @Override
    public List<Log> selectBusinessLog(String name) {
        return logDao.selectBusinessLog(name);
    }

    @Override
    public List<Log> selectBusinessLog() {
        return logDao.selectBusinessLog1();
    }

    @Override
    public void saveBusinessLog(String key, String url) {
        HashMap map = new HashMap<>();
        map.put("id",key);
        map.put("name","BusinessLog.log");
        map.put("time",new Date());
        map.put("address",url);
        map.put("type",2);
        logDao.saveLog(map);
    }

    @Override
    public Boolean logIsExist(String documentName) {
        if (logDao.logIsExist(documentName) == null) {
            return false;
        }
        return true;
    }

    @Override
    public Boolean cleanLoginLog() {
        return logDao.deleteLoginLog() >= 0;
    }

    @Override
    public void downloadLog(String fileName, HttpServletResponse response) throws IOException {
        String absolutePath = ResourceUtils.getURL("src\\log").getPath();
        String path = absolutePath + fileName;
        InputStream inputStream = new FileInputStream(path);
        response.reset();
        response.setContentType("application/octet-stream;charset=utf-8");
        String filename = new File(path).getName();
        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "utf-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        byte[] b = new byte[1024];
        int len;
        while ((len = inputStream.read(b)) > 0) {
            outputStream.write(b, 0, len);
        }
        try {
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (outputStream != null) {
            try {
                outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
